package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.utils.Car;
import com.example.demo.utils.CarCount;
import com.example.demo.utils.CarStoreCount;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CarMapper extends BaseMapper<Car> {
    @Select("SELECT cm.CarModelID, IFNULL(SUM(CASE WHEN c.storeID=#{storeID} AND c.CarStatus='可租' AND c.CarCondition='良好' THEN 1 ELSE 0 END), 0) as count FROM CarModel cm LEFT JOIN Car c ON cm.CarModelID = c.CarModelID GROUP BY cm.CarModelID")
    List<CarCount> selectCountByStoreID(String storeID);

    @Select("SELECT CarModelID ,Count(*) FROM Car c WHERE c.CarStatus='可租' AND c.CarCondition='良好' AND c.storeID=#{storeID} GROUP BY CarModelID")
    List<CarCount> selectCountByStoreID2(String storeID);
    @Select("SELECT cm.CarModelID, IFNULL(SUM(CASE WHEN c.storeID=#{storeID} AND c.CarModelID=#{CarModelID} AND c.CarStatus='可租' AND c.CarCondition='良好' THEN 1 ELSE 0 END), 0) as count FROM CarModel cm LEFT JOIN Car c ON cm.CarModelID = c.CarModelID WHERE cm.CarModelID=#{CarModelID} GROUP BY cm.CarModelID")
    CarCount selectByStoreIDCarModelID(String storeID, String CarModelID);

    @Select("SELECT * FROM Car c WHERE c.CarStatus='可租' AND c.CarCondition='良好' AND c.storeID=#{storeID}")
    List<Car> selectByStoreID(String storeID);

    @Select("SELECT c.*, cm.carModelID AS cm_carModelID, cm.cartype AS cm_cartype, cm.brand AS cm_brand, cm.price AS cm_price " +
            "FROM Car c " +
            "LEFT JOIN CarModel cm ON c.carModelID = cm.carModelID " +
            "WHERE c.CarStatus='可租' AND c.storeID=#{storeID}")
    @Results({
            @Result(property = "carID", column = "carID"),
            @Result(property = "carModelID", column = "carModelID"),
            @Result(property = "productionDate", column = "productionDate"),
            @Result(property = "licensePlateNumber", column = "licensePlateNumber"),
            @Result(property = "carCondition", column = "carCondition"),
            @Result(property = "carStatus", column = "carStatus"),
            @Result(property = "storeID", column = "storeID"),
            // 以下是 CarModel 的映射结果
            @Result(property = "carModel.carModelID", column = "cm_carModelID"),
            @Result(property = "carModel.carType", column = "cm_cartype"),
            @Result(property = "carModel.brand", column = "cm_brand"),
            @Result(property = "carModel.price", column = "cm_price")
    })
    List<Car> selectCarsWithModelsByStoreID(String storeID);

    @Select("SELECT * FROM Car c WHERE c.storeID=#{storeID} AND c.CarStatus='可租' AND c.CarCondition='良好' AND c.carModelID=#{carModelID} ORDER BY RAND() LIMIT 1")
    Car selectRandomAvailableCarByStoreIDAndCarModelID(@Param("storeID") String storeID, @Param("carModelID") String carModelID);

    @Update("UPDATE Car SET CarStatus=#{newCarStatus}, CarCondition=#{newCarCondition} WHERE CarID=#{carID}")
    void updateCarStatusAndCondition(@Param("carID") String carID,
                                    @Param("newCarStatus") String newCarStatus,
                                    @Param("newCarCondition") String newCarCondition);
    @Update("UPDATE Car SET storeID=#{storeID} WHERE CarID=#{carID}")
    void updateCarBelonging(@Param("carID") String carID, @Param("storeID") String storeID);
    @Select("SELECT c.storeID, c.CarModelID, COUNT(*) as count " +
            "FROM Car c " +
            "WHERE c.CarModelID = #{CarModelID} AND c.CarStatus='可租' AND c.CarCondition='良好' AND c.storeID!=#{storeID} " +
            "GROUP BY c.storeID " +
            "ORDER BY count DESC")
    List<CarStoreCount> selectCarCountByCarModelOrderByCountDesc(String CarModelID,@Param("storeID") String storeID);
}

